/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { describe } from '@jest/globals';

import { initByStageEmptyApp, initExecuteCommand, runShouldFail } from '../common/common';
import { ModuleBuildProfileModifier } from '../common/modifier/module-build-profile-modifier';
import { ModuleJsonModifier } from '../common/modifier/module-json-modifier';

/**
 * 业务校验相关的测试用例，用于测试对配置文件的校验是否正常
 */
describe('business validate test', () => {
  test.concurrent('Can not find product', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const commands = initExecuteCommand({
          mode: 'module',
          tasks: 'PreBuild',
          product: 'xxxx',
        });
        runShouldFail(dir, commands, 'Unable to find the product \'xxxx\'');
      }
    });
  });

  test.concurrent('RuntimeOS is invalid value', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const moduleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'entry');
        moduleBuildProfileModifier.updateTarget({
          name: 'default',
          runtimeOS: 'xxxxxx'
        }).outputSync();
        const commands = initExecuteCommand({
          mode: 'module',
          tasks: 'PreBuild',
        });
        runShouldFail(dir, commands, 'allowedValues: [ \'HarmonyOS\', \'OpenHarmony\' ]');
      }
    });
  });

  test.concurrent('Target deviceType should be a subset of module config', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const moduleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'entry');
        moduleBuildProfileModifier.updateTarget({
          name: 'default',
          config: {
            deviceType: ['car'],
          }
        }).outputSync();
        const commands = initExecuteCommand({
          mode: 'module',
          tasks: 'PreBuild',
        });
        runShouldFail(dir, commands, 'Make sure the deviceType field for the \'default\' target ' +
          'in the build-profile.json5 file is a subset of deviceType of the module.');
      }
    });
  });

  test.concurrent('Empty entryModules field - stage', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const moduleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'feature');
        moduleBuildProfileModifier.setEntryModules([]).outputSync();
        const commands = initExecuteCommand({
          mode: 'module',
          tasks: 'PreBuild',
        });
        runShouldFail(dir, commands, 'The entryModule field cannot be empty, because a feature module must ' +
          'have a corresponding entry module. This might happen when an entry module was removed.');
      }
    });
  });

  test.concurrent('startWindows - stage', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const moduleJsonModifier = new ModuleJsonModifier(dir, 'entry');
        moduleJsonModifier.updateAbility({
          'name': 'MainAbility',
          'srcEntry': './ets/MainAbility/MainAbility.ts',
          'description': '$string:MainAbility_desc',
          'icon': '$media:icon',
          'label': '$string:MainAbility_label',
          'startWindowIcon': undefined,
          'startWindowBackground': undefined,
          'visible': true,
          'skills': [
            {
              'entities': [
                'entity.system.home'
              ],
              'actions': [
                'action.system.home'
              ]
            }
          ]
        }).outputSync();
        const commands = initExecuteCommand({
          mode: 'module',
          tasks: 'default@PreBuild',
        });
        runShouldFail(dir, commands, 'For help about startWindows, you can solve the problem with this FAQ.');
      }
    });
  });
});
